import java.util.Stack;

public class EvaluateReversePolishNotation {
    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        for(String x : tokens) {
            if(!isOperator(x)) {
                // 数字直接入栈
                stack.push(Integer.valueOf(x));
            }else {
                int right = stack.pop();
                int left = stack.pop();
                switch(x) {
                    case "+":
                        stack.push(left + right);
                        break;
                    case "-":
                        stack.push(left - right);
                        break;
                    case "*":
                        stack.push(left * right);
                        break;
                    case "/":
                        stack.push(left / right);
                        break;
                    default:
                        break;
                }
            }
        }
        return stack.peek();
    }

    public boolean isOperator(String s) {
        // 判断是否是操作符
        return s.equals("+") || s.equals("-") || s.equals("*") || s.equals("/");
    }
}
